import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'

const Index = () => import(/* webpackChunkName: "base" */ '@/components/Index')

const OrderBase = () => import(/* webpackChunkName: "orders" */ '@/components/orders/Base')
const OrderIndex = () => import(/* webpackChunkName: "orders" */ '@/components/orders/Index')
const OrderShow = () => import(/* webpackChunkName: "orders" */ '@/components/orders/show')

const ReportIndex = () => import(/* webpackChunkName: "report" */ '@/components/report/Index')

const UserIndex = () => import(/* webpackChunkName: "users" */ '@/components/users/Index')

const BrandIndex = () => import(/* webpackChunkName: "brand" */ '@/components/brand/Index')
const BrandAudits = () => import(/* webpackChunkName: "brand" */ '@/components/brand/audits')
const BrandSales = () => import(/* webpackChunkName: "brand" */ '@/components/brand/sales')
const BrandData = () => import(/* webpackChunkName: "brand" */ '@/components/brand/data')
const BrandNewAduit = () => import(/* webpackChunkName: "brand" */ '@/components/brand/newAduit')

const ServerIndex = () => import(/* webpackChunkName: "server" */ '@/components/server/Index')

const ServiceIndex = () => import(/* webpackChunkName: "service" */ '@/components/service/Index')

const FinanceBase = () => import(/* webpackChunkName: "finance" */ '@/components/finance/Base')
const FinanceIndex = () => import(/* webpackChunkName: "finance" */ '@/components/finance/Index')
const FinanceRecharge = () => import(/* webpackChunkName: "finance" */ '@/components/finance/recharge')
const FinanceInvoiceSet = () => import(/* webpackChunkName: "finance" */ '@/components/finance/invoiceSet')

const Login = () => import(/* webpackChunkName: "auth" */ '@/components/auth/login')

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      component: Index,
      children: [
        {
          //报表
          path: '',
          meta: {
            auth: true
          },
          component: ReportIndex
        },
        {
          //这里是订单下面的路由
          path: 'orders',
          component: OrderBase,
          children: [
            {
              path: '/',
              name: 'orders',
              meta: {
                auth: true
              },
              component: OrderIndex
            },
            {
              path: ':id',
              name: 'order',
              meta: {
                auth: true
              },
              component: OrderShow
            }
          ]
        },
        {
          //这里是用户的路由
          path: 'users',
          name: 'users',
          meta: {
            auth: true
          },
          component: UserIndex
        },
        {
          //品牌商
          path: 'brand',
          component: BrandIndex,
          children: [
            {
              path: 'aduits',
              meta: {
                auth: true
              },
              name: 'brand-aduits',
              component: BrandAudits
            },
            {
              path: 'data',
              meta: {
                auth: true
              },
              name: 'brand-data',
              component: BrandData
            },
            {
              path: 'sales',
              meta: {
                auth: true
              },
              name: 'brand-sales',
              component: BrandSales
            },
            {
              path: 'new_aduit',
              name: 'brand-new-aduit',
              meta: {
                auth: true
              },
              component: BrandNewAduit
            }
          ]
        },
        {
          //服务商
          path: 'server',
          name: 'server',
          meta: {
            auth: true
          },
          component: ServerIndex
        },
        {
          //客服
          path: 'service',
          meta: {
            auth: true
          },
          component: ServiceIndex
        },
        {
          //财务
          path: 'finance',
          // name: 'finance',
          meta: {
            auth: true
          },
          component: FinanceBase,
          children: [
          		{
              path: '/',
              name: 'finance',
              meta: {
                auth: true
              },
              component: FinanceIndex
           	},
          		{
              path: ':id',
              name: 'recharge',
              meta: {
                auth: true
              },
              component: FinanceRecharge
            },
          		{
              path: 'invoiceSet',
              name: 'invoiceSet',
              meta: {
                auth: true
              },
              component: FinanceInvoiceSet
            }
          ]
        },
        {
          //系统
          path: 'system',
          name: 'system',
          meta: {
            auth: true
          },
          component: resolve => require(['@/components/system/Index'], resolve)
        },
        {
          //设置
          path: 'setting',
          meta: {
            auth: true
          },
          component: resolve => require(['@/components/setting/Index'], resolve)
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: resolve => require(['@/components/auth/login'], resolve)
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.meta.auth) {
    if (store.getters.current_user) {
      next()
    } else {
      next({
        path: '/login',
        query: {
          redirect: to.fullPath // 将跳转的路由path作为参数，登录成功后跳转到该路由
        }
      })
    }
  } else {
    next()
  }
})

export default router
